package com.example.xwattention.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.xwattention.mapper.AttentionMapper;
import com.example.xwcommon.entity.Attention;
import com.example.xwcommon.service.AttentionService;
import com.example.xwcommon.vo.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

@RestController(value = "attentionService")
public class AttentionServiceImpl implements AttentionService {
    @Autowired
    private AttentionMapper attentionMapper;
    @Override
    public Response<Integer> addAttention(String attentionName, String userName) {
        Attention attention = new Attention();
        attention.setAttentionName(attentionName);
        attention.setUserName(userName);
        Integer i =attentionMapper.insert(attention);
        if(i!=1){
            return new Response<>(500, "关注失败", null);
        }
        return new Response<>(200, "关注成功", i);
    }

    @Override
    public Response<Integer> removeAttention(Integer attentionId) {
        Integer i = attentionMapper.deleteById(attentionId);
        if(i!=1){
            return new Response<>(500, "取消关注失败", null);
        }
        return new Response<>(200, "取消关注成功", i);
    }

    @Override
    public Response<Attention> getAttentionById(Integer attentionId) {
        QueryWrapper<Attention> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("att_id", attentionId);
        Attention attention = attentionMapper.selectOne(queryWrapper);
        System.out.println("attention:"+attention);
        if(attention==null){
            return new Response<>(500, "查询失败", null);
        }
        return new Response<>(200, "查询成功", attention);
    }

    @Override
    public Response<Page<Attention>> getAttentionListByLoginName(Integer pageNum, Integer pageSize, String loginName) {
        Page<Attention> page = new Page<>(pageNum, pageSize);
        QueryWrapper<Attention> queryWrapper = new QueryWrapper<>();
        if(loginName!=null){
            queryWrapper.eq("att_user_loginname", loginName);
        }else{
            return new Response<>(5000, "参数为空", null);
        }
        Page<Attention> attentionPage = attentionMapper.selectPage(page, queryWrapper);
        if(attentionPage==null){
            return new Response<>(500, "查询失败", null);
        }
        return new Response<>(200, "查询成功", attentionPage);
    }
}
